package com.sys.object.common.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class ImageUrl {
	
	public static void main(String[] args) {
		ImageUrl imageUrl = new ImageUrl();
		/* 图片缩放 */
		imageUrl.imageHandle("http://g4.ykimg.com/1100641F4653E3ABB7711302A69FBF74856112-B68E-7600-D7A0-C999D91EF360", "e:\\image\\", "yy.jpg", "400*?;160*160");
		/* 图片剪切 */
//		imageUrl.cutImage("e:\\image\\xx.jpg", 10, 10, 0, 0);
	}
	
	public void cutImage(String fileName,int top,int bottom,int left,int right){
		File inputFile = new File(fileName); 
		int[] oldObj = ImgUtil.getFileWidthAndHeight(inputFile);
		try {
			ImgUtil.scissor(left,oldObj[0]-right,top,oldObj[1]-bottom,fileName,"e:\\image\\xx_X.jpg");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void imageHandle(String imgUrl,String filePath,String fileName,String format){
		/* 上传文件到目录 */
		File batchUpload = uploadUrlFileToDir(imgUrl,fileName,filePath);

		String fileExtName = fileName.substring(fileName.indexOf(".")+1, fileName.length());
		getImgFormat(filePath+fileName.substring(0,fileName.indexOf(".")),fileExtName,format);

	}
	
	
	/* 切图  */
	private String getImgFormat(String filePath,String fileExtName,String format){
		
		String fileName = filePath + "." +fileExtName;
		File inputFile = new File(fileName);  
		List<String[]> list = getImgFormat(inputFile,format);
		StringBuffer str = new StringBuffer();
		
//		String custom = "CUSTOM_"+model.getBusinessType();
//		String customImg = UploadPathConfig.getInstance().getPara(custom);

		for(String[] obj:list){
			int width = Integer.parseInt(obj[0]);  
	        int height = Integer.parseInt(obj[1]);
	        String widthName = obj[0];
	        String heightName = obj[1];
		       
	        if("?".equals(obj[2])){
	        	widthName = "X";
	        }
	        if("?".equals(obj[3])){
	        	heightName = "X";
	        }else if("$".equals(obj[3])){
	        	widthName = obj[2];
	        	heightName = "X";
	        }
	        String newFileName = filePath + "_" + widthName +"_" +heightName +"." +fileExtName;
	        File outFile = new File(newFileName);  
	        String outPath = outFile.getAbsolutePath();  
	        
	        if("gif".equals(fileExtName)){
	        	ImgUtil.resize(fileName, outPath, width, height, false, true ,"jpg"); 
	        }else if("jpeg".equals(fileExtName)){
	        	ImgUtil.resize(fileName, outPath, width, height, false, true,"png"); 
			}else{
	        	ImgUtil.resizeImage(inputFile, outPath, width, height, fileExtName); 
		    }

	        str.append(width);
	        str.append("*");
	        str.append(height);
	        str.append(";");
	        
		}
		
		return str.toString();
	}
	
	private List<String[]> getImgFormat(File inputFile,String format){

		List<String[]> list = new ArrayList<String[]>();
		if(format != null && !"".equals(format)){
			int[] oldObj = ImgUtil.getFileWidthAndHeight(inputFile);
			String[] formats = format.split(";");
			for(int i = 0;i<formats.length;i++){
				String[] fileFormats = formats[i].split("\\*");
				String widthstr = fileFormats[0];  
		        String heightstr = fileFormats[1];
		        if("?".equals(widthstr) && "?".equals(heightstr)){
		        	/* 错误定义 */
		        	continue;
		        }
		        int width = 0;
		        int height = 0;
		        

		        if("?".equals(widthstr)){
		        	height = Integer.parseInt(fileFormats[1]);
		            double bi = oldObj[1]/(double)height;
		            width = (int)(oldObj[0]/bi);
		        }else if("?".equals(heightstr)){
		        	width = Integer.parseInt(fileFormats[0]);
		            double bi = oldObj[0]/(double)width;
		            height = (int)(oldObj[1]/bi);
		        }else if("$".equals(heightstr)){
		        	if(oldObj[0] > oldObj[1]){
			        	width = Integer.parseInt(fileFormats[0]);
			            double bi = oldObj[0]/(double)width;
			            height = (int)(oldObj[1]/bi);
		        	}else{
			        	height = Integer.parseInt(fileFormats[0]);
			            double bi = oldObj[1]/(double)height;
			            width = (int)(oldObj[0]/bi);
		        	}
		        	widthstr = Integer.parseInt(fileFormats[0])+"";
		        }else{
					width = Integer.parseInt(fileFormats[0]);  
			        height = Integer.parseInt(fileFormats[1]);
		        }
		        String[] obj = new String[4];
		        obj[0] = width+"";
		        obj[1] = height+"";
		        obj[2] = widthstr;
		        obj[3] = heightstr;
		        list.add(obj);
			}
		}
		return list;
	}
	
	private File uploadUrlFileToDir(String urlName,String fileName,String uploadPath){
		File file = null;
		try{
			file = new File(uploadPath+fileName);
			if (file.exists()) {	//上传图片存在先删除
				if (!file.delete()) {
					return null;
				}
			}
			
			CreateFile.createPath(uploadPath);				//创建上传目录
			
			
			URL url =  new URL(urlName); 
			HttpURLConnection  httpUrl = (HttpURLConnection)url.openConnection(); 
	        httpUrl.connect(); 
	        BufferedInputStream in1 =  new   BufferedInputStream(httpUrl.getInputStream()); 
	        FileOutputStream out1 = new FileOutputStream(file); 
	
			byte[] bytes = new byte[1024];
			int c;
			while ((c = in1.read(bytes)) != -1) {
				out1.write(bytes, 0, c);
			}
			
			in1.close();
			out1.close();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}	
		return file;
		
	}
	
}
